<?php 
// 加载功能函数文件
require_once('helpers.php');


/*用户信息*/
$conn = @mysqli_connect('localhost','wd1900144','pass123456') or exit('网络开小差了,请稍后再试');
mysqli_select_db($conn,'wd1900144');
mysqli_query($conn,'set names utf8');

$act = $_REQUEST['act'];	// $_REQUEST 可以接收$_POST或者$_GET的数据

if($act=='reg'){
			unset($_GET['act']);
		$res = insert('car_user',$_GET);	
	
		 echo $res>0 ? json_encode(['error'=>0,'msg'=>'注册成功']) : json_encode(['error'=>1,'msg'=>'网络出错']);
	}
if($act=='update'){
			$credit = $_GET['credit'];
			$id = $_GET['id'];
			$info = getOne("select * from car_user where uid=$id");
			$newCridet = number_format($info['credit'])+$credit;
			
		if($credit<0&&$newCridet>0){
		
		
			// var_dump($newCridet);
			// var_dump($info);
			mysqli_query($conn,"UPDATE car_user SET `credit` = '$newCridet' WHERE `car_user`.`uid` = $id");
			echo json_encode(['error'=>0,'msg'=>'扣除积分成功','credit'=>$newCridet]) ;
		}
		else if ($newCridet<=0){
			
				echo json_encode(['error'=>2,'msg'=>'积分不够']) ;
		}
		else{

				mysqli_query($conn,"UPDATE car_user SET `credit` = '$newCridet' WHERE `car_user`.`uid` = $id");
			echo json_encode(['error'=>0,'msg'=>'累积积分成功','credit'=>$newCridet]);
		}
			
	}
if($act=='mydate'){
	$uid = $_GET['uid'];
	unset($_GET['act']);
	$uid = update('car_user',$_GET);//使用insert函数一定要添加全部字段，不能添加一个字段，一般直接传 $_GET 或者$_POST
	echo json_encode(['error'=>0,'msg'=>'success','uid'=>$uid]);
}
if($act=='order'){
	$look = $_GET['look'];
	if($look!=1){
		unset($_GET['act']);
		unset($_GET['look']);
		$uid = insert('car_order',$_GET);//使用insert函数一定要添加全部字段，不能添加一个字段，一般直接传 $_GET 或者$_POST
		
	}

	
	$count = getCount('car_order');
	$res = 30 - $count['c'];
	echo json_encode(['error'=>0,'msg'=>'success','res'=>$res]);
	
	
}

//查看预约信息 
else if($act=='list'){
	

	$limit = $_GET['limit'];	// 每页显示多少条
	$pn = $_GET['pn'];	// 页码
	$start = ($pn-1)*$limit;
	// 获取分类列表数据
	
	$cid = $_GET['cid'];
	$tab = array();
	// $json = json_decode(['cid'=>1,'name'=>'预约申请列表']);
	$tab[0] = json_encode(['cid'=>1,'name'=>'预约申请列表']);
	if($cid==0){
		
		// var_dump($tab[0]['cid']);


			$list = getAll("select * from car_order where 1 limit $start,$limit");
			$allist = array();
			$allist[0] = $list;
			
			$is = 'default';
		}else{
			$allist = getAll("select * from car_order where 1  limit $start,$limit");
			$count = getCount('car_order');
			$is = 'update';
		}

	echo json_encode(['error'=>0,'msg'=>'success','list'=>$allist,'cate'=>[['cid'=>'1','name'=>'预约申请列表']],'is'=>$is,'count'=>$count]);
	
}

else if($act=='edit'){
		unset($_GET['act']);
		$state = $_GET['state'];
		$oid = $_GET['oid'];
		$res = update('car_order',$_GET,"oid=$oid");
		
		 echo $res>0 ? json_encode(['error'=>0,'msg'=>'操作成功']) : json_encode(['error'=>1,'msg'=>'未操作']);
}
else if($act=='del'){
		unset($_GET['act']);
		$oid = $_GET['oid'];
		$res = delete('car_order',"oid=$oid");
		
		 echo $res>0 ? json_encode(['error'=>0,'msg'=>'退回成功']) : json_encode(['error'=>1,'msg'=>'未退回']);
}


if($act=='login'){
		$phone = $_POST['phone'];
		$password = $_POST['password'];
		$uinfo = getOne("select * from car_user where phone='$phone'&&password='$password'");
		// var_dump($uinfo);
		echo $uinfo != NULL ? json_encode(['error'=>0,'msg'=>'success','uinfo'=>$uinfo]) : json_encode(['error'=>1,'msg'=>'用户名或密码出错']);
}

if($act=='pwd'){
	
}

if($act=='cal'){
	$day = $_GET['day'];
	unset($_GET['act']);
	// var_dump($day);
	$caid = insert('car_cal',$_GET);//使用insert函数一定要添加全部字段，不能添加一个字段，一般直接传 $_GET 或者$_POST
	echo json_encode(['error'=>0,'msg'=>'success','caid'=>$caid]);
}
if($act=='callook'){
	$val = $_GET['val'];
	$num = getAll("select * from car_cal where day LIKE '%$val%'");
	
	
	$total = getCount('car_cal');

	echo json_encode(['error'=>0,'msg'=>'success','num'=>$num,'total'=>$total]);
}

?>